﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Shapes;
using System.Windows.Media;

namespace Sonce.Editor.Tools
{
    public class ShapeTool : IEditorTool
    {
        //TODO: Only rectangle drawing is currently implemented. Implement other shapes then rename 'private' to 'public' on the next line.
        private Shapes Shape = Shapes.Rectangle;
        private double lineWidth = 1.0;
        //private Color color;

        #region Enumeration
        public enum Shapes
        {
            Rectangle,
            Square,
            Ellipse,
            Circle,
            Triangle,
            Polygon,
            RectangleAsPolyLine, //For easier editing (move particular nodes rather than resize)
        }
        #endregion

        #region Constructors
        public ShapeTool()
        {
        }

        public ShapeTool(Shapes shape)
        {
            if (shape != Shapes.Rectangle)
            {
                throw new NotImplementedException("Selected shape is not implemented: " + shape.ToString());
            }
            this.Shape = shape;
        }
        #endregion

        #region ITool Members
        public Editor Editor
        {
            get;
            set;
        }

        public bool KeyDown(Key key)
        {
            return false;
        }

        public bool KeyUp(Key key)
        {
            return false;
        }

        public bool MouseDown(Point position)
        {
            return false;
        }

        public bool MouseUp(Point position)
        {
            this.Editor.LayerPreview.Clear();
            this.Editor.SelectionManager.UnselectAll();
            //System.Windows.Shapes.Rectangle rectangle;
            //rectangle = this.Editor.LayerWorking.DrawRectangle(this.Editor.StartPoint, position);
            //this.Editor.UnselectAll();
            //this.Editor.Select(rectangle);
            //Draw rectangle as polyline

            //this.Editor.LayerWorking.StartPolyLine(this.Editor.StartPoint);
            //this.Editor.LayerWorking.ContinuePolyLine(new Point(position.X, this.Editor.StartPoint.Y));
            //this.Editor.LayerWorking.ContinuePolyLine(new Point(position.X, position.Y));
            //this.Editor.LayerWorking.ContinuePolyLine(new Point(this.Editor.StartPoint.X, position.Y));
            //this.Editor.LayerWorking.ClosePolyLine(true);

            //this.Editor.Select();
            return true;
        }

        public bool MouseMove(Point position, bool isMouseDown)
        {
            if (isMouseDown)
            {
                DrawRectangle(this.Editor.LayerPreview, this.Editor.StartPoint, position);
            }
            return true;
        }

        public bool MouseEnter()
        {
            return false;
        }

        public bool MouseLeave()
        {
            return false;
        }

        public bool MouseWheel(int delta)
        {
            return false;
        }
        #endregion

        #region Elements
        /// <summary>
        /// Draw a rectangle at the specified location.
        /// </summary>
        /// <param name="point1">Location of the first corner.</param>
        /// <param name="point2">Location of the last corner.</param>
        /// <returns>Returns the drawn visual object for futher customization.</returns>
        /// <remarks>First point could be any of the four points, it does NOT need to be the upper left.</remarks>
        public Rectangle DrawRectangle(Layer layer, Point point1, Point point2)
        {
            //Create rectangle
            Rectangle rectangle = new Rectangle();
            double x = (point2.X - point1.X > 0 ? point1.X : point2.X);
            double y = (point2.Y - point1.Y > 0 ? point1.Y : point2.Y);
            rectangle.Width = Math.Abs(point2.X - point1.X);
            rectangle.Height = Math.Abs(point2.Y - point1.Y);
            rectangle.Stroke = layer.StrokeBrush;
            rectangle.Fill = layer.FillBrush;
            rectangle.Stretch = Stretch.Fill; //Must be Stretch.Fill
            layer.Draw(rectangle, new Point(x, y));
            return rectangle;
        }
        #endregion
    }
}
